package com.slipper.system.license.service;

import com.slipper.core.cache.CacheKey;
import com.slipper.core.cache.CacheUtil;
import com.slipper.exception.CommonException;
import com.slipper.util.*;
import org.springframework.stereotype.Service;
import jakarta.annotation.Resource;

import com.slipper.system.license.dao.LicenseDao;
import com.slipper.system.license.bo.LicenseBO;

import com.slipper.core.transaction.ReadTransactional;
import com.slipper.core.transaction.WriteTransactional;

import java.math.BigDecimal;
import java.util.List;
import java.util.Map;


@Service
@ReadTransactional
public class LicenseService {
    @Resource
    private LicenseDao licenseDao;
    
    public LicenseBO queryBy() {
        LicenseBO license = CacheUtil.get(CacheKey.License.KEY, LicenseBO.class);
        if (license == null) {
            List<LicenseBO> licenseList = licenseDao.queryList();
            if (licenseList.isEmpty()) {
                license = createProbation();
                licenseDao.insert(license);
            } else {
                license = licenseList.getFirst();
            }
            CacheUtil.set(CacheKey.License.KEY, license, 60 * 60);
        }

        Map<String, Object> dataMap = LicenseUtil.parseLicense(license.getLicense());
        long expireTime = new BigDecimal(String.valueOf(dataMap.get("expireTime"))).longValue();
        license.setLicenseVersion((String) dataMap.get("licenseVersion"));
        license.setLicenseTime(new BigDecimal(String.valueOf(dataMap.get("licenseTime"))).longValue());
        license.setExpireTime(expireTime);
        license.setValid(expireTime > System.currentTimeMillis());
        return license;
    }

    @WriteTransactional
    public int updateLicense(LicenseBO bo) {
        if (Tool.isBlank(bo.getLicense())) return 0;

        int result = 0;
        List<LicenseBO> licenseList = licenseDao.queryList();
        if (!licenseList.isEmpty()) {
            LicenseBO licenseBO = licenseList.getFirst();
            Map<String, Object> dataMap = LicenseUtil.parseLicense(bo.getLicense().trim());
            if (Tool.isBlank(dataMap.get("machineCode"))
                    || !dataMap.get("machineCode").equals(licenseBO.getMachineCode())) {
                throw new CommonException("授权码不正确！");
            }
            long expireTime = new BigDecimal(String.valueOf(dataMap.get("expireTime"))).longValue();
            if (expireTime < System.currentTimeMillis()) throw new CommonException("无效的授权码！");
            licenseBO.setLicense(bo.getLicense().trim());
            licenseBO.setLicenseVersion((String) dataMap.get("licenseVersion"));
            licenseBO.setLicenseTime(new BigDecimal(String.valueOf(dataMap.get("licenseTime"))).longValue());
            licenseBO.setExpireTime(expireTime);
            licenseBO.setUpdateTime(System.currentTimeMillis());
            result = licenseDao.update(licenseBO);
        }
        CacheUtil.remove(CacheKey.License.KEY);
        return result;
    }

    private LicenseBO createProbation() {
        try {
            String machineCode = MachineUtil.getBase64Code();
            String license = LicenseUtil.createLicense(machineCode);
            Map<String, Object> dataMap = LicenseUtil.parseLicense(license);

            LicenseBO licenseBO = new LicenseBO();
            licenseBO.setId(UUID.getUUID());
            licenseBO.setLicense(license);
            licenseBO.setMachineCode(machineCode);
            licenseBO.setLicenseVersion((String) dataMap.get("licenseVersion"));
            licenseBO.setLicenseTime(new BigDecimal(String.valueOf(dataMap.get("licenseTime"))).longValue());
            licenseBO.setExpireTime(new BigDecimal(String.valueOf(dataMap.get("expireTime"))).longValue());
            licenseBO.setUpdateTime(System.currentTimeMillis());
            licenseBO.setCreateTime(System.currentTimeMillis());
            return licenseBO;
        } catch (Exception e) {
            throw new CommonException("生成试用期授权码出错！", e);
        }
    }
}
